package com.sssoft.Yundian.activity;

import java.util.List;
import com.sssoft.Yundian.R;
import com.sssoft.Yundian.Dao.TypeDao;
import com.sssoft.Yundian.bean.Other;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class AddtypeActivity extends Activity{
    //需要适配的数据集合
    private List<Other> list;
    //数据库增删改查操作类
    private TypeDao dao;
    private EditText nameET;
    private EditText balanceET;
    //适配器
    private MyAdapter adapter;
    //
    private ListView accountLV;
    private Button button;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addtype);
        //初始化控件
        initView();
        dao=new TypeDao(this);
        //从数据库查询出所有数据
        list=dao.queryAll();

        adapter=new MyAdapter();
        accountLV.setAdapter(adapter);//给LISTVIEW添加适配器(自动吧数据生成条目)
        
        
        
        button = (Button) findViewById(R.id.bt_f);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(AddtypeActivity.this, AddGoodsActivity.class);
				startActivity(intent);
				finish();
			}
		});
    }
    //初始化控件
    private void initView(){
        accountLV=(ListView) findViewById(R.id.accountLV);
        nameET=(EditText) findViewById(R.id.nameET);
      
    }
    //activity_main.xml对应image view的点击事件触发的方法
    public void add(View view){

        String name=nameET.getText().toString().trim();
     
        //三木运算balance.equals("")则等于0
        //如果balance不是空字符串，则进行类型转换
        Other a=new Other(name);
        dao.insert(a);        //插入数据库
        list.add(a);          //插入集合
        
        accountLV.setAdapter(adapter);     //刷新界面
        //选中最后一个
        accountLV.setSelection(accountLV.getCount()-1);
        nameET.setText("");
      
    }
    private class MyAdapter extends BaseAdapter{
        public int getCount(){
            return list.size();
        }
        public Object getItem(int position){
            return list.get(position);
        }
        public long getItemId(int position){
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent){
            //重用convertView
            View item=convertView!=null?convertView:View.inflate(getApplicationContext(),R.layout.item,null);
            //获取该试图中的textView
//            TextView idTV=(TextView) item.findViewById(R.id.idTV);
            TextView nameTV=(TextView) item.findViewById(R.id.nameTV);
            
            //根据当前位置获取account对象
            final Other a=list.get(position);

            //把account对象中的数据放到textview中

//            idTV.setText(a.getId()+"");
            nameTV.setText(a.getName());
                    
           
            ImageView deleteIV=(ImageView) item.findViewById(R.id.deleteTV);
            
            

            //删除图片的点击事件触发的方法
            deleteIV.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    //删除数据之前先弹出一个对话框
                    android.content.DialogInterface.OnClickListener listener=
                            new android.content.DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog,int which) {
                            list.remove(a);      //从集合中删除
                            dao.delete(a.getId());  //从数据库中删除
                            notifyDataSetChanged();//刷新界面
                        }
                    };
                    //创建对话框
                    AlertDialog.Builder builder=new AlertDialog.Builder(AddtypeActivity.this);
                    builder.setTitle("确定要删除吗？");  //设置标题
                    //设置确定按钮的文本及监听
                    builder.setPositiveButton("确定",listener);
                    builder.setNegativeButton("取消",null); //设置取消按钮
                    builder.show();
                }
            });
            return item;
        }
    }
 

}